<?php
class fily_KeyValueArray {
    /**
     *
     * @var array
     */
    protected $array = array();

    /**
     * the output format
     * {0} is key {1} is value
     * @var <type>
     */
    protected $outputFormat = '{0}="{1}"';
    /**
     * returns a array item
     * @param string $key
     * @return object
     */
    public function get($key) {
        $result = null;
        if(isset($this->array[$key])) {
            $result =$this->array[$key];
        }
        return $result;
    }

    /**
     * sets a array item
     * @param string $key
     * @param object $value
     */
    public function set($key, $value) {
        $this->array[$key] = $value;
    }

    /**
     * clears the array
     */
    public function clear() {
        $this->array = array();
    }

    /**
     * remove a item
     * @param string $key
     */
    public function remove($key) {
        unset($this->array[$key]);
    }

    /**
     * returns the count of the array
     * @return int
     */
    public function count() {
        return count($this->array);
    }

    /**
     * merge this array with another array
     * @param array $array
     */
    public function merge(array $array) {
        $this->array = array_merge($array, $this->array);
    }

    /**
     * checks if this array is empty
     * @return Boolean
     */
    public function isEmpty() {
        return ($this->count() == 0);
    }

    /**
     * returns the output format
     * @return string
     */
    public function getOutputFormat() {
        return $this->outputFormat;
    }

    /**
     * sets the output format
     * {0} for key and {1} for value
     * @param string $outputFormat
     */
    public function setOutputFormat($outputFormat) {
        $this->outputFormat =$outputFormat;
    }

    /**
     * convert array to string
     * @return string
     */
    public function toString() {
        $html = '';
        if (!empty ($this->array)) {
            foreach ($this->array as $key => $value) {
                if($value!=null){
                    if($value instanceof fily_KeyValueArray) {
                        if(!$value->isEmpty()) {
                            $value = $value->toString();
                        }
                        else {
                            $value="";
                        }
                    }


                    $keyValueOutput = $this->outputFormat;
                    $keyValueOutput = str_replace('{0}',  strtolower($key), $keyValueOutput);
                    $keyValueOutput = str_replace('{1}', $value, $keyValueOutput);
                    $html .= ' '. $keyValueOutput;
                }
            }
        }
        return $html;
    }



}

?>
